package com.zmzncs.lmtc.module.park.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.common.pojo.ChnnelsBody;
import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.daoer.GateOnOff;
import com.zmzncs.lmtc.common.pojo.tengda.AllChannels;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.common.util.TengdaUtil;
import com.zmzncs.lmtc.module.park.dao.ParkEntranceDao;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDaoerDao;
import com.zmzncs.lmtc.module.park.pojo.park_entrance.ParkChannels;
import com.zmzncs.lmtc.module.park.pojo.park_entrance.ParkEntrance;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfo;
import com.zmzncs.lmtc.module.park.service.ParkEntranceService;
import com.zmzncs.lmtc.module.park.service.ParkInfoDaoerService;
import lombok.extern.slf4j.Slf4j;
import org.HdrHistogram.SingleWriterRecorder;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JD
 * @since 2020-11-09
 */
@Slf4j
@Service
public class ParkEntranceServiceImpl extends ServiceImpl<ParkEntranceDao, ParkEntrance> implements ParkEntranceService {

    @Autowired
    ParkEntranceDao parkEntranceDao;
    @Autowired
    ParkInfoDao parkInfoDao;
    @Autowired
    ParkInfoDaoerService parkInfoDaoerService;
    @Autowired
    ParkInfoDaoerDao parkInfoDaoerDao;

    @Autowired
    HttpServletRequest request;


    @Override
    public Map<String,Object> getDaoerChannels(Long parkId, Integer parkSource) {

        Map<String,Object> map = new HashMap<>();
        List<ParkChannels> parkChannels = new ArrayList<>();

        if (parkSource == 1){
            map.put("status",0);
            map.put("msg","此停车场无法查询通道信息，请更换停车场");
            log.error("此停车场无法查询通道信息，请更换停车场。");
        }

        //道尔停车场
        if (parkSource == 3) {
            ChnnelsBody channels = parkInfoDaoerService.getDaoerChannels(parkId);
            if (channels != null){
                parkChannels = JsonUtil.listTransform(channels.getBody(), ParkChannels.class);
                map.put("status",1);
                map.put("msg","成功");
            }
        }

        //腾达
        if (parkSource == 4) {
            ParkInfo parkInfo = parkInfoDao.selectById(parkId);
            List<AllChannels> channels = TengdaUtil.getChannels(parkInfo.getBolianParkId());
            if (channels != null && channels.size() > 0){
                log.info("查询腾达通道信息成功");
                map.put("status",1);
                map.put("msg","成功");
                for (AllChannels channel : channels) {
                    ParkChannels parkChannel = new ParkChannels();
                    parkChannel.setChannelId(channel.getChannelId().toString());
                    parkChannel.setChannelName(channel.getChannelName());
                    Integer inOutType = channel.getInOutType();
                    //道闸通道类型  0 入口 1出口
                    if (inOutType == 1 || inOutType == 3){
                        parkChannel.setType("0");
                    } else {
                        parkChannel.setType("1");
                    }
                    parkChannels.add(parkChannel);
                }
            } else {
                log.info("查询腾达停车场信息失败");
                map.put("status",0);
                map.put("msg","未查询到车场道闸通道信息");
            }

        }
        map.put("channels",parkChannels);
        return map;
    }

    @Override
    public Map<String,Object> parkGateOnOff(Long parkId, Integer parkSource, String channelId, Integer channelIdStatus) {
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        Map<String,Object> map = new HashMap<>();
        if (parkSource == 1){
            map.put("status",0);
            map.put("msg","此停车场无法抬杆，请更换停车场");
        }
        //道尔停车场
        if (parkSource == 3) {
            GateOnOff gateOnOff = parkInfoDaoerService.daoerGateOnOff(parkId, channelId, channelIdStatus);
            GateOnOff.Head head = gateOnOff.getHead();
            map.put("status",head.getStatus());
            map.put("msg",head.getMessage());
            log.info("道尔停车场手动抬杆成功，通道Id：" + channelId + ",通道状态：" + channelIdStatus);
        }

        //腾达
        if (parkSource == 4) {
            ParkInfo parkInfo = parkInfoDao.selectById(parkId);
            Boolean onOff = TengdaUtil.gateOnOff(parkInfo.getBolianParkId(), channelId, channelIdStatus, loginUser.getNickname());
            if (onOff){
                map.put("status",1);
                map.put("msg","操作成功");
                log.info("腾达手动抬杆成功，通道Id：" + channelId + ",通道状态：" + channelIdStatus);
            } else {
                map.put("status",0);
                map.put("msg","操作失败");
                log.info("腾达手动抬杆成功，通道Id：" +channelId + ",通道状态：" + channelIdStatus);
            }
        }

        return map;
    }
}
